library(readxl)
library(tidyverse)PA 4: Military Spending
Today you will be tidying messy data to explore the relationship between countries of the world and military spending. You can find the gov_spending_per_capita.xlsx data included in the data folder.
This task is complex. It requires many different types of abilities. Everyone will be good at some of these abilities but nobody will be good at all of them. In order to produce the best product possible, you will need to use the skills of each member of your group.
Data Description
We will be using data from the Stockholm International Peace Research Institute (SIPRI). The SIPRI Military Expenditure Database is an open source data set containing time series on the military spending of countries from 1949–2019. The database is updated annually, which may include updates to data from previous years.
Military expenditure is presented in many ways:
- in local currency and in US $ (both from 2018 and current);
- in terms of financial years and calendar years;
- as a share of GDP and per capita.
The availability of data varies considerably by country, but we note that data is available from at least the late 1950s for a majority of countries that were independent at the time. Estimates for regional military expenditure have been extended backwards depending on availability of data, but no estimates for total world military expenditure are available before 1988 due to the lack of data from the Soviet Union.
SIPRI military expenditure data is based on open sources only.
Data Import
1. Using your warm-up handout, fill in the code below to read the military expenditures data into your workspace.
# This code should be identical to what you wrote down from the lecture activity!
military <- read_xlsx("gov_spending_per_capita.xlsx",
sheet = ,
skip = ,
n_max = ,
na = c()
)Error:
! `path` does not exist: 'gov_spending_per_capita.xlsx'
Filtering Unwanted Rows
If you give the Country column a look, you’ll see there are names of continents and regions included. These names are only included to make it simpler to find countries, as they contain no data.
Luckily for us, these region names were also stored in the “Regional totals” sheet. We can use the Region column of this dataset to filter out the names we don’t want.
Run the code below to read in the “Regional totals” data.
cont_region <- read_xlsx(here::here("data",
"gov_spending_per_capita.xlsx"),
sheet = "Regional totals",
skip = 14) |>
filter(Region != "World total (including Iraq)",
Region != "World total (excluding Iraq)")Error:
! `path` does not exist: 'C:/Users/cann4817/Desktop/spring-2026/data/gov_spending_per_capita.xlsx'
We can use the pull() function to extract just the values of the column Region. The output of the pull() function is a vector of values (not a data frame).
regions <- cont_region |>
pull(Region)Error:
! object 'cont_region' not found
Then we can use the vector of regions to filter out (exclude) the rows in the military dataset that contain regions instead of countries.
military_clean <- military |>
filter(! Country %in% regions)Error:
! object 'military' not found
2. Write a sentence describing what the line of code filter(!Country %in% regions) is doing in the context of the data.
Insert Answer Here
Canvas Question #1
3. Complete the code below to figure out what four regions were NOT removed from the military_clean data set?
Hint: the regions that were not removed have missing values for every column except Country.
military_clean |>
filter(if_all(.cols = _________, #hint: what is the easiest way to include every column except `Country`
.fns = __________) #hint: what function in R (there are several) tests if a value is missing or is NA?
)Error in parse(text = input): <text>:2:26: unexpected input
1: military_clean |>
2: filter(if_all(.cols = __
^
Data Organization
We are interested in comparing the military expenditures of countries in Eastern Europe. Our desired plot looks something like this:
Unfortunately, if we want a point representing the spending for every country and year, we need every year to be a single column!
To tidy a dataset like this, we need to pivot the columns of years from wide format to long format. To do this process we need three arguments:
cols: The set of columns that represent values, not variables. In these data, those are all the columns from1988to2019.names_to: The name of the variable that should be created to move these columns into. In these data, this could be"Year".values_to: The name of the variable that should be created to move these column’s values into. In these data, this could be labeled"Spending".
These form the three required arguments for the pivot_longer() function.
6. Pivot the cleaned up military data set to a “longer” orientation. Save this new “long” version as a new object called military_long.
Hint: Do not overwrite your cleaned up dataset!
Data Visualization
Now that we’ve transformed the data, let’s create a plot to explore military spending across Eastern European countries.
7. Create side-by-side boxplots to explore the military spending between Eastern European countries.
Hint 1: Place the Country variable on an axis that makes it easier to read the labels!
Hint 2: Make sure you change the plot title and axis labels to accurately represent the plot.
Hint 3: Make sure you change the plot title and axis labels to accurately represent the plot.
# Countries to include in the plot!
eastern_europe <- c("Armenia",
"Azerbaijan",
"Belarus",
"Georgia",
"Moldova",
"Russia",
"Ukraine")Canvas Question 2 & Question 3
8. Looking at the plot you created above, which Eastern European country had the second highest median military expenditure?.
9. Looking at the plot you created above, which Eastern European country had the largest variability in military expenditures over time?